package component.fy.com.common_module.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by FuYu on 2017/1/17.
 * 需要在Application中初始化
 */

public class SharedPreDataBase {
    private static SharedPreDataBase sharedPreDataBase;
    private static SharedPreferences preferences;
    private static SharedPreferences.Editor editor;

    private SharedPreDataBase(Context context) {
        preferences = context.getSharedPreferences("bowleshopping", Context.MODE_PRIVATE);
        editor = preferences.edit();
    }

    public static SharedPreDataBase getPreferences(Context context) {
        if (sharedPreDataBase == null) {
            sharedPreDataBase = new SharedPreDataBase(context);
        }
        return sharedPreDataBase;
    }

    /**
     * 存储String字符串
     *
     * @param key
     * @param value
     */
    public static void saveStringData(String key, String value) {
        editor.putString(key, value);
        editor.apply();//异步执行操作
        editor.commit();
    }

    /**
     * 取出String字符串
     *
     * @param key
     * @return
     */
    public static String getStringData(String key) {
        String content = preferences.getString(key, "");
        return content;
    }

    public static void saveIntData(String key, int value) {
        editor.putInt(key, value);
        editor.apply();
        editor.commit();
    }

    /**
     * 获取int数据,默认-1
     *
     * @param key
     * @return
     */
    public static int getIntData(String key) {
        return preferences.getInt(key, -1);
    }

    public static void saveBooleanData(String key, boolean value) {
        editor.putBoolean(key, value);
        editor.apply();
        editor.commit();
    }

    /**
     * 获取boolean数据
     *
     * @param key
     * @return
     */
    public static boolean getBooleanData(String key) {
        return preferences.getBoolean(key, true);
    }

    /**
     * 获取boolean数据
     *
     * @param key
     * @param defvalue
     * @return
     */
    public static boolean getBooleanData(String key, boolean defvalue) {
        return preferences.getBoolean(key, defvalue);
    }

    public static void saveFloatData(String key, float value) {
        editor.putFloat(key, value);
        editor.apply();
        editor.commit();
    }

    /**
     * 获取float数据
     *
     * @param key
     * @return
     */
    public static float getFloatData(String key) {
        return preferences.getFloat(key, -1);
    }

    /**
     * 清除指定数据
     *
     * @param key
     */
    public static void removeData(String key) {
        editor.remove(key);
        editor.commit();
    }

    /**
     * 清空数据
     */
    public static void clearData() {
        editor.clear();
        editor.commit();
    }
}
